package com.example.ele_guarantee_hh_pro.comclass;


import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.CCService;
import com.cloudcc.deveditor.core.UserInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.List;

/*pc端地图查看后台*/
public class FindMapInfoTest {


    private UserInfo userInfo;
    private CCService cs;

    public FindMapInfoTest(UserInfo userInfo) {
        this.userInfo = userInfo;
        cs = new CCService(userInfo);
    }

    public String findMap(String latitude) {


        String userId = userInfo.getUserId();
        String[] split = latitude.split(",");
//       经度
        String lon = split[0];
//       纬度
        String lat = split[1];

//        上下0.5
        Double new_lat = Double.valueOf(lat);
        Double new_Add_lat = new_lat + 0.5;
        Double new_deleted_lat = new_lat - 0.5;
        Double new_lon = Double.valueOf(lon);
        Double new_Add_lon = new_lon + 0.5;
        Double new_deleted_lon = new_lon - 0.5;
//        String FINDACCOUNTTBPCSQL = "select jyzx from ghjl where yh ='" + userId + "' and is_deleted='0' ";
//        List<CCObject> ghjl = cs.cqlQuery("ghjl", FINDACCOUNTTBPCSQL);
//        String jyzx = "";
//        if (ghjl.size() > 0) {
//             jyzx += "(";
//            int k = 0;
//            for (int i = 0; i < ghjl.size(); i++) {
//                String jyzxIds = ghjl.get(i).get("jyzx") == null ? "" : ghjl.get(i).get("jyzx") + "";
//                jyzx+=jyzxIds;
//                k++;
//                if (k<ghjl.size()){
//                    jyzx+=",";
//                }
//            }
//            jyzx+=")";
//        }
        String findManage_groupSql = "select id,fk_account,lxdz,dlwz,fk_ccuser,fk_psmc,dlwzco,dlwzla from manage_group where dlwzla > '" + new_deleted_lat + "' and dlwzla < '" + new_Add_lat + "' and dlwzco > '" + new_deleted_lon + "' and dlwzco < '" + new_Add_lon + "' and is_deleted='0' and fk_ccuser ='" + userId + "' and ghzt!='已失效' ";

        List<CCObject> manageGroup = cs.cqlQuery("manage_group", findManage_groupSql);

        JSONObject returnDataInfo = new JSONObject();
        JSONArray AccountInfoList = new JSONArray();
        JSONArray EleoppInfoList = new JSONArray();
        if (manageGroup.size() > 0) {
            for (int i = 0; i < manageGroup.size(); i++) {
                String fk_ccuser = manageGroup.get(i).get("fk_ccuser") == null ? "" : manageGroup.get(i).get("fk_ccuser") + "";
                String fk_psmc = manageGroup.get(i).get("fk_psmc") == null ? "" : manageGroup.get(i).get("fk_psmc") + "";
                String manage_lxdz = manageGroup.get(i).get("lxdz") == null ? "" : manageGroup.get(i).get("lxdz") + "";
                String manage_dlwz = manageGroup.get(i).get("dlwz") == null ? "" : manageGroup.get(i).get("dlwz") + "";
                String fk_account = manageGroup.get(i).get("fk_account") == null ? "" : manageGroup.get(i).get("fk_account") + "";
                String id = manageGroup.get(i).get("id") == null ? "" : manageGroup.get(i).get("id") + "";
                String dlwzco = manageGroup.get(i).get("dlwzco") == null ? "" : manageGroup.get(i).get("dlwzco") + "";
                String dlwzla = manageGroup.get(i).get("dlwzla") == null ? "" : manageGroup.get(i).get("dlwzla") + "";
                String findEleNum = "select frequece from ele_num where fk_psmc ='" + fk_psmc + "' and fk_account ='" + fk_account + "' and is_deleted='0' and ownerid='"+userId+"'";
                String pc = "";
                List<CCObject> ele_num = cs.cqlQuery("ele_num", findEleNum);
//                if (!jyzx.equals("")){
//                    String findmanage_group = "select frequece from ele_num where fk_account ='"+fk_account+"' and fk_psmc in "+jyzx+"  and is_deleted='0' ";
//                    List<CCObject> ele_num1 = cs.cqlQuery("ele_num", findmanage_group);
//                    if (ele_num1.size()>0){
//
//                    }
//                }

                if (ele_num.size() > 0) {
                    pc = ele_num.get(0).get("frequece") == null ? "" : ele_num.get(0).get("frequece") + "";
                }

                // String findAccountInfo = "select name,follow_status,deal_status,registeredAddress,id,khdd from Account where khdd ='" + manage_lxdz + "' and  is_deleted='0'";
                String findAccountInfo = "select name,follow_status,deal_status,registeredAddress,id,khdd from Account where id ='"+fk_account+"' ";
                List<CCObject> ccObjects = cs.cqlQuery("Account", findAccountInfo);
                if (ccObjects.size() > 0) {
                    for (int j = 0; j < ccObjects.size(); j++) {
                        JSONObject AccountInfo = new JSONObject();
                        AccountInfo.put("name", ccObjects.get(j).get("name"));
                        AccountInfo.put("khgjzt", ccObjects.get(j).get("follow_status"));
                        AccountInfo.put("lxdz", manage_lxdz);
                        AccountInfo.put("khkhzt", ccObjects.get(j).get("deal_status"));
                        AccountInfo.put("accountId", ccObjects.get(j).get("id"));
                        AccountInfo.put("jwd", dlwzco+","+dlwzla);
                        AccountInfo.put("GroupId", id);

                        AccountInfo.put("tbpc", pc);
                        AccountInfoList.add(AccountInfo);
                    }
                }

                String findEleoppInfo = "select name,jieduan,frequence,contact_address,id from eleopp where contact_address = '" + manage_lxdz + "' and is_deleted='0' and ownerid ='" + userId + "' and status1!='已完结'";
                List<CCObject> eleopp = cs.cqlQuery("eleopp", findEleoppInfo);
                if (eleopp.size() > 0) {
                    for (int j = 0; j < eleopp.size(); j++) {
                        JSONObject eleoppInfoData = new JSONObject();
                        eleoppInfoData.put("name", eleopp.get(j).get("name"));
                        eleoppInfoData.put("lxdz", eleopp.get(j).get("contact_address"));
                        eleoppInfoData.put("jd", eleopp.get(j).get("jieduan"));
                        eleoppInfoData.put("jwd", dlwzco+","+dlwzla);
                        eleoppInfoData.put("tbpc", eleopp.get(j).get("frequence"));
                        eleoppInfoData.put("eleoppId", eleopp.get(j).get("id"));
                        eleoppInfoData.put("GroupId", id);
                        EleoppInfoList.add(eleoppInfoData);
                    }
                }


            }
            returnDataInfo.put("code", "200");
            returnDataInfo.put("sql", findManage_groupSql);
            returnDataInfo.put("Account", AccountInfoList);
            returnDataInfo.put("Eleopp", EleoppInfoList);
        } else {
            returnDataInfo.put("code", "200");
            returnDataInfo.put("sql", findManage_groupSql);
            returnDataInfo.put("Account", AccountInfoList);
            returnDataInfo.put("Eleopp", EleoppInfoList);
        }

        return String.valueOf(returnDataInfo);


    }

    //更新地址
    public String updateAddress(String data) {
        String userId = userInfo.getUserId();
        JSONObject jsonObject = JSONObject.fromObject(data);
        JSONObject JS = new JSONObject();
        String fk_psmc_id = "";
        String fk_account_id = "";
        String fk_ccuser_id = "";
        ImportAccount importAccount = new ImportAccount(userInfo);
        try {
            if (jsonObject.size() > 0) {
                //管户小组id
                String id = jsonObject.getString("id") == null ? "" : jsonObject.getString("id") + "";
                //地址
                String address = jsonObject.getString("address") == null ? "" : jsonObject.getString("address") + "";
                //存地理位置（经纬度）
                String gen = "";
                if (!"null".equals(address)){
                    if (!"".equals(address)){
                        try {
                            gen = importAccount.gen(address);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        String[] split = gen.split(",");
                        //经度
                        String lon = split[0];
                        //纬度
                        String lat = split[1];
                        //根据管户小组id查询公共资源交易中心、关联客户、管户经理
                        String manage_group_sql = "select fk_psmc,fk_account,fk_ccuser from manage_group where id='" + id + "' " +
                                "and is_deleted='0'";
                        List<CCObject> manage_group_list = cs.cqlQuery("manage_group", manage_group_sql);

                        if (manage_group_list.size() > 0) {
                            fk_psmc_id = manage_group_list.get(0).get("fk_psmc") == null ? "" : manage_group_list.get(0).get("fk_psmc") + "";
                            fk_account_id = manage_group_list.get(0).get("fk_account") == null ? "" : manage_group_list.get(0).get("fk_account") + "";
                            fk_ccuser_id = manage_group_list.get(0).get("fk_ccuser") == null ? "" : manage_group_list.get(0).get("fk_ccuser") + "";
                            String manage_group_update = "update manage_group set lxdz='"+address+"'" +
                                    ",dlwzla='"+lat+"'" +
                                    ",dlwzco='"+lon+"'" +
                                    "where fk_account='" + fk_account_id + "' " +
                                    "and is_deleted='0' " +
                                    "and fk_ccuser='" + fk_ccuser_id + "' " +
                                    "and fk_psmc='" + fk_psmc_id + "'";
                            cs.cqlQuery("manage_group", manage_group_update);

                            String account_sql ="update Account set khdd='"+address+"' " +
                                    "where id='"+fk_account_id+"' " +
                                    "and is_deleted='0'";
                            cs.cqlQuery("Account",account_sql);

                            //更改电子保函商机地址
                            String updateEleoppAddress = "update eleopp set contact_address ='" + address + "' where khmc='" + fk_account_id + "' and is_deleted='0' " +
                                    "and fk_psmc='" + fk_psmc_id + "' ";
                            cs.cqlQuery("eleopp", updateEleoppAddress);
                            JS.put("code", "200");
                            JS.put("msg", "更改地址成功");
                        } else {
                            JS.put("code", "129");
                            JS.put("msg", "管户小组未找到");
                        }
                    }else {
                        JS.put("code", "128");
                        JS.put("msg", "地址不能为空");
                    }
                }else {
                    JS.put("code", "128");
                    JS.put("msg", "地址不能为空");
                }
            } else {
                JS.put("code", "404");
                JS.put("msg", "未找到参数,更改地址失败");
            }
        } catch (Exception e) {
            JS.put("msg", e.toString());
        }
        return String.valueOf(JS);
    }

    //添加活动（事件）
    //id,其它字段，
    public String addevent(String data){
        JSONObject jsonObject = JSONObject.fromObject(data);
        //相关项id
        String relateid = jsonObject.getString("id");
        //类型
        String type = jsonObject.getString("type");
        //被分配人
        String belongtoid = jsonObject.getString("belongtoid");
        //主题(下拉)
        String sub = jsonObject.getString("sub");
        //主题
        String subject = jsonObject.getString("subject");
        //事件名称
        String position = jsonObject.getString("position");
        //内容
        String remark = jsonObject.getString("remark");
        //电子保函推荐事件
        String recordtype = "20211390340C7F7B9Sa4";

        String obj_suffix = "";
        if (!"".equals(relateid)) {
            obj_suffix = relateid.substring(0, 3);
        }
        String sql2 = "select SCHEMETABLE_NAME api from tp_sys_object where prefix='" + obj_suffix + "'";
        List<CCObject> list2 = cs.cqlQuery("works", sql2);
        String api = "";
        if (list2.size() > 0) {
            api = list2.get(0).get("api") == null ? "" : list2.get(0).get("api") + "";
        }
        JSONObject jsonObject1 = new JSONObject();
        try {
            if ("Account".equals(api)){
                CCObject event = new CCObject("Event");
                event.put("type", type);
                event.put("belongtoid", belongtoid);
                event.put("sub", sub);
                event.put("subject", subject);
                event.put("position", position);
                event.put("remark", remark);
                event.put("recordtype", recordtype);
                event.put("ownerid", userInfo.getUserId());
                cs.insert(event);
                jsonObject1.put("msg","success");
                jsonObject1.put("code","200");
            }else if ("eleopp".equals(api)){
                CCObject event = new CCObject("Event");
                event.put("type", type);
                event.put("belongtoid", belongtoid);
                event.put("sub", sub);
                event.put("subject", subject);
                event.put("position", position);
                event.put("remark", remark);
                event.put("recordtype", recordtype);
                event.put("ownerid", userInfo.getUserId());
                cs.insert(event);
                jsonObject1.put("msg","success");
                jsonObject1.put("code","200");
            }else {
                jsonObject1.put("msg","failure");
                jsonObject1.put("code","404");
            }
        }catch (Exception e){
            jsonObject1.put("msg",e.toString());
        }
        return String.valueOf(jsonObject1);
    }



}
